package com.lmpop.trade.ti_goods;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.FileIO;
import com.lmpop.trade.ts_module.Ts_moduleInfo;
import com.lmpop.updateHtml.parseXml.Config;
import com.lmpop.updateHtml.updateHtml.UpdateArticleHtml;

public class Ti_goodsInfo implements TradeInterf
{
  QueryList queryList;
  UpdateArticleHtml uah;
  FileIO io;
  Config conf;
  String rootpath;

  public Ti_goodsInfo()
  {
    this.queryList = new QueryList();
    this.uah = new UpdateArticleHtml();
    this.io = new FileIO();
    this.conf = new Config();
    this.conf.init();
    this.rootpath = this.conf.getString("rootpath");
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_goods ti_goods = new Ti_goods();
    ti_goods.setGoods_id(map.getString("goods_id"));
    ti_goods.setGoods_no(map.getString("goods_no"));
    ti_goods.setCust_id(map.getString("cust_id"));
    ti_goods.setClass_attr(map.getString("class_attr"));
    ti_goods.setArea_attr(map.getString("area_attr"));
    ti_goods.setBrand_id(map.getString("brand_id"));
    ti_goods.setGoods_name(map.getString("goods_name"));
    ti_goods.setMember_price(map.getString("member_price"));
    ti_goods.setOrg_price(map.getString("org_price"));
    ti_goods.setMarket_price(map.getString("market_price"));
    ti_goods.setSale_price(map.getString("sale_price"));
    ti_goods.setSend_inter(map.getInt("send_inter"));
    ti_goods.setBuy_inter(map.getInt("buy_inter"));
    ti_goods.setWeight(map.getString("weight"));
    ti_goods.setStock_in(map.getInt("stock_in"));
    ti_goods.setStock_num(map.getInt("stock_num"));
    ti_goods.setStock_out(map.getInt("stock_out"));
    ti_goods.setStock_down(map.getInt("stock_down"));
    ti_goods.setPrice_unit(map.getString("price_unit"));
    ti_goods.setState_code(map.getString("state_code"));

    ti_goods.setShip_fare(map.getString("ship_fare"));
    ti_goods.setFull_discount(map.getString("full_discount"));
    ti_goods.setF_amountship(map.getString("f_amountship"));
    ti_goods.setF_amountdiscount(map.getString("f_amountdiscount"));
    ti_goods.setFull_presented(map.getString("full_presented"));
    ti_goods.setBuyandsend(map.getString("buyandsend"));
    ti_goods.setFree_vouchers(map.getString("free_vouchers"));
    ti_goods.setKeywords(map.getString("keywords"));
    ti_goods.setOutline(map.getString("outline"));
    ti_goods.setGoods_desc(map.getString("goods_desc"));
    ti_goods.setRemark(map.getString("remark"));
    ti_goods.setLink_goods(map.getString("link_goods"));
    ti_goods.setLink_fit(map.getString("link_fit"));
    ti_goods.setAttr_desc(map.getString("attr_desc"));

    ti_goods.setPublish_user_id(map.getString("publish_user_id"));
    ti_goods.setDiscount_id(map.getString("discount_id"));
    String result_code = "0";
    try {
      insert(dbmMgr, ti_goods);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_goods ti_goods)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_goods", ti_goods);
  }
  public SysMap create_alterPage(DbmInterf dbmMgr, SysMap map) {
    this.uah.updateArticleByGoodsId("r0824lf8xUk501u", map.getString("goods_id"), "0", "1");
   // this.uah.updateArticleByGoodsId("7212356b1j38fNO", map.getString("goods_id"), "0", "1");
    return map;
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    Ts_moduleInfo moduleInfo = new Ts_moduleInfo();
    String modulepath = moduleInfo.getSavaPathByCode("goods");
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        List list = null;
        String publish_date = "";
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals("")) {
            list = this.queryList.getList("getTi_goodsByPk", pkStr[i]);
            publish_date = ((Hashtable)list.get(0)).get("publish_date").toString();
            publish_date = publish_date.substring(0, 10);
            delete(dbmMgr, pkStr[i]);
            FileIO.DelFile(this.rootpath + modulepath + publish_date + "/" + pkStr[i] + ".html");
          }
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    map.setString("attach_root_id", pkid_string);
    map.setString("attach_sign", "delete");

    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_goods where goods_id=#value#
    dbmMgr.update("deleteTi_goods", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_goods ti_goods = new Ti_goods();
    ti_goods.setGoods_id(map.getString("goods_id"));
    ti_goods.setGoods_no(map.getString("goods_no"));
    ti_goods.setCust_id(map.getString("cust_id"));
    ti_goods.setClass_attr(map.getString("class_attr"));
    ti_goods.setArea_attr(map.getString("area_attr"));
    ti_goods.setBrand_id(map.getString("brand_id"));
    ti_goods.setGoods_name(map.getString("goods_name"));
    ti_goods.setMember_price(map.getString("member_price"));
    ti_goods.setOrg_price(map.getString("org_price"));
    ti_goods.setMarket_price(map.getString("market_price"));
    ti_goods.setSale_price(map.getString("sale_price"));
    ti_goods.setSend_inter(map.getInt("send_inter"));
    ti_goods.setBuy_inter(map.getInt("buy_inter"));
    ti_goods.setWeight(map.getString("weight"));
    ti_goods.setStock_in(map.getInt("stock_in"));
    ti_goods.setStock_num(map.getInt("stock_num"));
    ti_goods.setStock_out(map.getInt("stock_out"));
    ti_goods.setStock_down(map.getInt("stock_down"));
    ti_goods.setPrice_unit(map.getString("price_unit"));
    ti_goods.setState_code(map.getString("state_code"));

    ti_goods.setShip_fare(map.getString("ship_fare"));
    ti_goods.setFull_discount(map.getString("full_discount"));
    ti_goods.setF_amountship(map.getString("f_amountship"));
    ti_goods.setF_amountdiscount(map.getString("f_amountdiscount"));
    ti_goods.setFull_presented(map.getString("full_presented"));
    ti_goods.setBuyandsend(map.getString("buyandsend"));
    ti_goods.setFree_vouchers(map.getString("free_vouchers"));

    ti_goods.setKeywords(map.getString("keywords"));
    ti_goods.setOutline(map.getString("outline"));
    ti_goods.setGoods_desc(map.getString("goods_desc"));
    ti_goods.setRemark(map.getString("remark"));
    ti_goods.setLink_goods(map.getString("link_goods"));
    ti_goods.setLink_fit(map.getString("link_fit"));
    ti_goods.setAttr_desc(map.getString("attr_desc"));

    ti_goods.setPublish_user_id(map.getString("publish_user_id"));
    ti_goods.setDiscount_id(map.getString("discount_id"));
    String result_code = "0";
    try {
      update(dbmMgr, ti_goods);
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_goods ti_goods)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_goods", ti_goods);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Hashtable params = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    params.put("start", Integer.valueOf(start));
    params.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByTi_goodsPage", params);
  }

  public List getGoodsList(Object obj)
  {
    Hashtable params = (Hashtable)obj;
    return this.queryList.getList("getListByTi_goods", params);
  }

  public int getCountByObj(Object obj)
  {
    Hashtable params = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTi_goods", params);
    int count = 0;
    if ((list != null) && (list.size() > 0))
    {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_goodsByPk", pk);
  }

  public String getGoodToSelect(String type, String cat_id, String cust_id, String goods_name, String brand_id)
  {
    String selectItems = "<select name=\"" + type + "_select1\" id=\"" + type + "_select1\" size=\"10\" style=\"width:90%\" multiple=\"true\">";
    Ti_goods ti_goods = new Ti_goods();
    if (!cat_id.equals("")) ti_goods.setClass_attr(cat_id);
    if (!cust_id.equals("")) ti_goods.setCust_id(cust_id);
    if (!goods_name.equals("")) ti_goods.setGoods_name(goods_name);
    if (!brand_id.equals("")) ti_goods.setBrand_id(brand_id);

    List list = this.queryList.getList("getTi_goodsForSelect", ti_goods);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String goods_id = ""; String s_goods_name = "";
        if (map.get("goods_id") != null) {
          goods_id = map.get("goods_id").toString();
        }
        if (map.get("goods_name") != null) {
          s_goods_name = map.get("goods_name").toString();
        }
        selectItems = selectItems + "<option value='" + goods_id + "'>" + s_goods_name + "</option>";
      }
    }

    selectItems = selectItems + "</select>";
    return selectItems;
  }

  public List getListForTable(String cat_id, String cust_id, String goods_name, String brand_id)
  {
    Ti_goods ti_goods = new Ti_goods();
    if (!cat_id.equals("")) ti_goods.setClass_attr(cat_id);
    if (!cust_id.equals("")) ti_goods.setCust_id(cust_id);
    if (!goods_name.equals("")) ti_goods.setGoods_name(goods_name);
    if (!brand_id.equals("")) ti_goods.setBrand_id(brand_id);
    System.out.println("=========>" + ti_goods.getCust_id());
    return this.queryList.getList("getTi_goodsForSelect", ti_goods);
  }

  public SysMap updateState(DbmInterf dbmMgr, SysMap map)
  {
    String pk_state = map.getString("pk_state");
    String result_code = "0";
    try {
      if (!pk_state.equals(""))
      {
        String[] pkStr = pk_state.split(",");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] p_s = pkStr[i].split("\\|");
          Hashtable psMap = new Hashtable();
          psMap.put("goods_id", p_s[0]);
          psMap.put("state_code", p_s[2].trim());
          updateState(dbmMgr, psMap);
        }
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateState(DbmInterf dbmMgr, Hashtable psMap)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_goodsState", psMap);
  }

  public SysMap updateGoodsState(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String state_code = map.getString("state_code");
    String remark = map.getString("remark");
    String result_code = "0";
    try
    {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
        {
          Hashtable psMap = new Hashtable();
          psMap.put("goods_id", pkStr[i]);
          psMap.put("state_code", state_code);
          psMap.put("remark", remark);
          if (pkStr[i].equals("")) continue; updateState(dbmMgr, psMap);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    return map;
  }

  public String getGoodsNameById(String pk)
  {
    List gList = this.queryList.getList("getTi_goodsNameById", pk);
    String goods_name = "";
    if ((gList != null) && (gList.size() > 0))
    {
      Hashtable gmap = (Hashtable)gList.get(0);
      if (gmap.get("goods_name") != null)
      {
        goods_name = gmap.get("goods_name").toString();
      }
    }

    return goods_name;
  }

  public String getLinkSelectByIds(String ids)
  {
    String selectItems = "";
    String[] gIds = ids.split("\\|");
    for (int i = 0; i < gIds.length; i++)
    {
      String name = getGoodsNameById(gIds[i]);
      if (name.equals(""))
        continue;
      selectItems = selectItems + "<option value='" + gIds[i] + "'>" + name + "</option>";
    }

    return selectItems;
  }

  public SysMap updateStockNum(DbmInterf dbmMgr, SysMap map)
  {
    Ti_goods ti_goods = new Ti_goods();
    ti_goods.setGoods_id(map.getString("goods_id"));
    ti_goods.setStock_num(map.getInt("stock_num"));
    String result_code = "0";
    try {
      updateStockNum(dbmMgr, ti_goods);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateStockNum(DbmInterf dbmMgr, Ti_goods ti_goods)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_goodsStockNum", ti_goods);
  }

  public SysMap updatePrice(DbmInterf dbmMgr, SysMap map)
  {
    String selmethod = map.getString("selmethod");
    String result_code = "0";
    try {
      if (selmethod.equals("0"))
      {
        String pkid_string = map.getString("pkid");
        String allprice = map.getString("allprice");
        String memberprice = map.getString("memberprice");
        String[] pkStr = pkid_string.split("\\|");

        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          Hashtable psMap = new Hashtable();
          psMap.put("goods_id", pkStr[i]);
          psMap.put("sale_price", allprice);
          psMap.put("member_price", memberprice);
          updatePrice(dbmMgr, psMap);
        }
      }

      if (selmethod.equals("1"))
      {
        String pk_price = map.getString("pk_price");
        String[] pkStr = pk_price.split("\\:");

        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] p_s = pkStr[i].split("=");
          if (p_s.length <= 2)
            continue;
          Hashtable psMap = new Hashtable();
          psMap.put("goods_id", p_s[0]);

          psMap.put("sale_price", p_s[1].trim());
          psMap.put("member_price", p_s[2].trim());
          updatePrice(dbmMgr, psMap);
        }

      }

    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatePrice(DbmInterf dbmMgr, Hashtable psMap)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_goodsPrice", psMap);
  }

  public int getSend_interById(String pk)
  {
    List gList = this.queryList.getList("getTi_goodsSend_interById", pk);
    int send_inter = 0;
    String tempData = "";
    if ((gList != null) && (gList.size() > 0))
    {
      Hashtable gmap = (Hashtable)gList.get(0);
      if (gmap.get("send_inter") != null)
      {
        tempData = gmap.get("send_inter").toString();
        send_inter = Integer.parseInt(tempData);
      }
    }

    return send_inter;
  }

  public String getGoodsIn_dateById(String pk)
  {
    List gList = this.queryList.getList("getGoodsIn_dateById", pk);
    String publish_date = "";
    if ((gList != null) && (gList.size() > 0))
    {
      Hashtable gmap = (Hashtable)gList.get(0);
      if (gmap.get("publish_date") != null)
      {
        publish_date = gmap.get("publish_date").toString();
      }
    }

    return publish_date;
  }

  public String getGoodsListCount(Hashtable cMap)
  {
    List uList = this.queryList.getList("getTi_goodsAllCount", cMap);
    String counter = "0";
    if ((uList != null) && (uList.size() > 0))
    {
      Hashtable map = (Hashtable)uList.get(0);
      if (map.get("goods_id") != null)
      {
        counter = map.get("goods_id").toString();
      }
    }
    return counter;
  }

  public SysMap updateSend_inter(DbmInterf dbmMgr, SysMap map)
  {
    String send_intermethod = map.getString("send_intermethod");
    String result_code = "0";
    try {
      if (send_intermethod.equals("0"))
      {
        String pkid_string = map.getString("pkid");
        String allsend_inter = map.getString("allsend_inter");
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          Hashtable psMap = new Hashtable();
          psMap.put("goods_id", pkStr[i]);
          psMap.put("send_inter", allsend_inter);
          updateSend_inter(dbmMgr, psMap);
        }
      }

      if (send_intermethod.equals("1"))
      {
        String pk_send_inter = map.getString("pk_send_inter");
        String[] pkStr = pk_send_inter.split(",");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] p_s = pkStr[i].split("\\|");
          if (p_s.length <= 1)
            continue;
          Hashtable psMap = new Hashtable();
          psMap.put("goods_id", p_s[0]);
          psMap.put("send_inter", p_s[1].trim());
          updateSend_inter(dbmMgr, psMap);
        }

      }

    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateSend_inter(DbmInterf dbmMgr, Hashtable psMap)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_goodsSend_inter", psMap);
  }

  public SysMap updateStockNumByOP(DbmInterf dbmMgr, SysMap map)
  {
    if ((map.getString("order_state").equals("2")) || (map.getString("order_state").equals("5"))) {
      Map ti_goods = new Hashtable();
      ti_goods.put("goods_id", map.getString("goods_id"));
      ti_goods.put("stock_out", Integer.valueOf(map.getInt("vary_num")));
      String result_code = "0";
      try {
        updateStockNumByOP(dbmMgr, ti_goods);
      } catch (ApplicationException e) {
        e.printStackTrace();
        result_code = "1";
      }
      map.setString("result_code", result_code);
    }
    return map;
  }

  public void updateStockNumByOP(DbmInterf dbmMgr, Map ti_goods)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_goodsStockNumByOP", ti_goods);
  }

  public Map getGoodsByClassAttr(String classattr) {
    String good_id = ""; String good_name = ""; String stock_num = "";
    Map goodsmap = new Hashtable();
    List list = this.queryList.getList("getGoodsByClassAttr", classattr);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("goods_name") != null) {
          good_name = map.get("goods_name").toString();
        }
        if (map.get("goods_id") != null) {
          good_id = map.get("goods_id").toString();
        }
        if (map.get("stock_num") != null) {
          stock_num = map.get("stock_num").toString();
        }
        goodsmap.put(good_id, good_name + "|" + stock_num);
      }
    }
    return goodsmap;
  }

  public List getHotGoodsList()
  {
    return this.queryList.getList("getHotListByTi_goods");
  }

  public List getHotGoodsList(Map params)
  {
    return this.queryList.getList("getHotListByTi_goods", params);
  }
  public List getNewGoodsList() {
    return this.queryList.getList("getNewListByTi_goods");
  }
  public List getNewGoodsList(Map params) {
    return this.queryList.getList("getNewListByTi_goods", params);
  }
}